<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Dialog } from '@/components/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTable'
import { Icon } from '@/components/Icon'
import { ref, unref, reactive } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import BasicManageApi from '@/api/linePatrol/Basicmanage'
import Detail from './components/Detail.vue'
import Write from './components/Write.vue'
//引入安装的依赖
const ids = ref<string[]>([])
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const i = await BasicManageApi.ParameterList({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize)
    })
    return {
      list: i.rows,
      total: i.total
    }
  },
  fetchDelApi: async () => {
    const res = await BasicManageApi.DeletParameter(unref(ids)[0])
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods

const { t } = useI18n()
const delLoading = ref(false)
//删除
const delData = async (row: any | null) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row ? [row.id] : elTableExpose?.getSelectionRows().map((v: any) => v.id) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'parameter',
    label: t('参数名称'),
    search: {
      hidden: true
    },
    detail: {
      span: 24
    },
    form: {
      hidden: false,
      colProps: {
        span: 20
      }
    }
  },
  {
    field: 'describe',
    label: t('描述'),
    search: {
      hidden: true
    },
    detail: {
      span: 24
    },
    form: {
      hidden: false,
      colProps: {
        span: 20
      }
    }
  },
  {
    field: 'parKey',
    label: t('键'),
    search: {
      hidden: true
    },
    form: {
      hidden: false,
      colProps: {
        span: 20
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'parValue',
    label: t('值'),
    search: {
      hidden: true
    },
    form: {
      hidden: false,
      colProps: {
        span: 20
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'action',
    label: t('tableDemo.action'),
    width: 200,
    fixed: 'right',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <span class="TableBtn" onClick={() => action(data.row, 'Detail')}>
                {t('查看')}
              </span>
              <span
                class="TableBtn"
                style="color:rgb(212, 111, 13)"
                onClick={() => action(data.row, 'Edit')}
              >
                {t('修改')}
              </span>
              <span class="TableBtn" style="color:#f56c6c" onClick={() => delData(data.row)}>
                {t('删除')}
              </span>
            </>
          )
        }
      }
    }
  }
])

// @ts-ignore
const { allSchemas } = useCrudSchemas(crudSchemas)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const currentRow: any = ref([])
const actionType = ref('')
const saveLoading = ref(false)
//表格操作
const action = (row: any, type: string) => {
  actionType.value = type
  dialogVisible.value = true //打开弹窗
  currentRow.value = row
  if (type == 'add') {
    dialogTitle.value = '新增'
  } else if (type == 'Edit') {
    dialogTitle.value = '修改'
  } else {
    dialogTitle.value = '查看'
  }
}
const WriteRef = ref()
const save = async () => {
  const write = unref(WriteRef)
  const formData = await write?.submit()
  if (formData) {
    saveLoading.value = true
    saveLoading.value = false
    const res: any = ref(false)
    //新增任务组
    if (actionType.value == 'add') {
      res.value = await BasicManageApi.AddParameterData(formData)
        .catch(() => {})
        .finally(() => {
          saveLoading.value = false
        })
    } else {
      //修改字
      res.value = await BasicManageApi.EditParameterData(formData)
        .catch(() => {})
        .finally(() => {
          saveLoading.value = false
        })
    }
    if (res.value) {
      dialogVisible.value = false
      currentPage.value = 1
      getList()
    }
  }
}
</script>

<template>
  <ContentWrap title="巡线参数管理" style="position: relative">
    <BaseButton size="small" type="primary" @click="action({}, 'add')">
      <Icon icon="ep:plus" :size="13" />新增
    </BaseButton>
    <Table
      id="table"
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      row-key="uuid"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
  <Dialog style="width: 600px" v-model="dialogVisible" :title="dialogTitle">
    <Detail
      v-if="actionType == 'Detail'"
      :detail-schema="allSchemas.detailSchema"
      :current-row="currentRow"
    />
    <Write
      v-if="actionType !== 'Detail'"
      ref="WriteRef"
      :form-schema="allSchemas.formSchema"
      :current-row="currentRow"
    />
    <template #footer>
      <BaseButton
        v-if="actionType !== 'Detail'"
        type="primary"
        :loading="saveLoading"
        @click="save"
      >
        {{ t('exampleDemo.save') }}
      </BaseButton>
      <BaseButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</BaseButton>
    </template>
  </Dialog>
</template>
<style scoped>
.el-collapse-item {
  padding: 15px 0;
}
.el-collapse-item__content {
  padding-bottom: 0;
}
.el-collapse-item__header {
  width: 62px;
  height: 24px;
  border-radius: 2px;
  color: white;
  background: #409eff;
  justify-content: center;
  font-size: 12px;
}
.el-collapse-item__arrow {
  margin: 0 2px;
}

:deep(.TableBtn) {
  margin: 0 5px;
  cursor: pointer;
  color: #409eff;
  font-weight: 600;
}
</style>
